package com.easondongh.blog.service.impl;

import com.easondongh.blog.mapper.LinkMapper;
import com.easondongh.blog.pojo.Link;
import com.easondongh.blog.service.LinkService;
import com.easondongh.blog.utils.DateTimeUtils;
import com.easondongh.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 友情链接
 * @author EasonDongH
 * @date 2020/3/26 20:24
 */
@Service
public class LinkServiceImpl implements LinkService {

    private static final String[] VALID_URL_PREFIX = {"http://", "https://"};

    @Autowired
    private LinkMapper linkMapper;

    @Override
    public void save(Link link) {
        String dateStr = DateTimeUtils.DateToString(new Date());
        link.setCreatedTime(dateStr);
        link.setUpdateTime(dateStr);
        if(!link.getLinkUrl().startsWith(VALID_URL_PREFIX[0]) && !link.getLinkUrl().startsWith(VALID_URL_PREFIX[1])) {
            link.setLinkUrl(VALID_URL_PREFIX[0] + link.getLinkUrl());
        }
        linkMapper.save(link);
    }

    @Override
    public void update(Link link) {
        String dateStr = DateTimeUtils.DateToString(new Date());
        link.setUpdateTime(dateStr);
        linkMapper.update(link);
    }

    @Override
    public Link getById(Integer id) {
        return linkMapper.getById(id);
    }

    @Override
    public List<Link> getAll() {
        return linkMapper.getAll();
    }

    @Override
    public void deleteById(Integer id) {
        linkMapper.deleteById(id);
    }
}
